%% Replication file of Baqaee, Burstein and Koike-Mori (2023) %%%%%%%%%%%%%%
% Prepared by Yasutaka Koike-Mori (UCLA)
% Description: Draw other figures and tables. create a database with the Stata file before running the file.

clear all
close all
addpath('functions')
addpath('subroutines')
addpath('data') 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Figure O1
run("subroutines/sub_gen_married.m")
run("subroutines/sub_Figure_O1a.m")
run("subroutines/sub_gen_young_old.m")
run("subroutines/sub_Figure_O1b.m")

%% Figure O3
clear all
run("subroutines/MatlabDataConstruction_short.m")
run("subroutines/sub_Figure_O3_pre.m")
run("subroutines/sub_Figure_O3.m")

%% Figure O4
clear all
load 'data/DataforMatlab/UK_Budget.mat'
load 'data/DataforMatlab/pvec_tmp.mat'
subset = [1:9 11:16];
Bx_vec = sum(B_vec(subset,:,:),1);
Bxi_vec = B_vec(subset,:,:)./Bx_vec;
[I,N,T] = size(Bxi_vec);
pvec = reshape(pvec_tmp(:,subset)',[I,1,T]);
% Constant sigma
sigmaM = 0.5;
[U_vec,WP_total_vec, WP_FS_vec] = CalMoneyMetric_Unobserved(I_vec, Bx_vec, Bxi_vec, pvec, sigmaM, 0);
run("subroutines/sub_Figure_O4a.m")
run("subroutines/sub_Figure_O4b.m")

% IV version
run("subroutines/sub_engel_IV.m")
[U_vec,WP_total_vec, WP_FS_vec] = CalMoneyMetric_Unobserved(I_vec, Bx_vec, Bxi_vec, pvec, sigmaM, 0);
run("subroutines/sub_Figure_O4c.m")
run("subroutines/sub_Figure_O4d.m")
clear all
%% Figure O.5
clear all
load data/DataforMatlab/torn.csv
load data/price/ONSCDEF.csv
run("subroutines/sub_Figure_O5.m")
%% Figure O.6
clear all
load 'data/DataforMatlab/UK_Budget.mat'
load 'data/DataforMatlab/pvec_tmp.mat'
B_vec = B_vec./sum(B_vec);
I = size(B_vec,1);N = size(B_vec,2);T = size(B_vec,3);
pvec = reshape(pvec_tmp',[I,1,T]);
[U_vec] = CalMoneyMetric(I_vec, B_vec, pvec,0);
run("subroutines/sub_Figure_O6.m")

%% Table O.1&3
clear all
run("subroutines/sub_TableO1.m")
run("subroutines/sub_TableO3.m")

%% Table O.4-7
clear all
run("subroutines/sub_TableO4.m")
run("subroutines/sub_TableO5.m")
run("subroutines/sub_TableO6.m")
run("subroutines/sub_TableO7.m")

%% Bootstrap
%clear all
%run("subroutines/sub_UKB.m") % take a lot of time